package la.iok.hzsvn.lewin.movie.core.operation.model;

import la.iok.hzsvn.share.tools.datetime.DateTimeUtil;

import java.util.Date;
import java.util.Objects;

public class ChargeOrderBase {
    /**
     * 支付中
     */
    public static final int STATE_PAYING = 1;
    /**
     * 已支付
     */
    public static final int STATE_PAID = 2;
    /**
     * 订单已经关闭
     */
    public static final int STATE_CLOSED = 3;
    /**
     * 已经退款
     */
    public static final int STATE_REFUND = 4;

    /**
     * 微信支付
     */
    public static final int PAY_TYPE_WX = 1;
    /**
     * 支付宝支付
     */
    public static final int PAY_TYPE_ALIPAY = 2;
    /**
     * 管理员充值,不一定通过真实的微信或者支付宝方式充值
     */
    public static final int PAY_TYPE_ADMIN = 3;
    /**
     * id
     */
    private Long id;
    /**
     * 下单时间
     */
    private Date prepareDate;
    /**
     * 有效支付时长,单位秒,超时订单不能支付
     */
    private Integer expiresIn;
    /**
     * 支付方式,值为{@link #PAY_TYPE_WX}或者{@link #PAY_TYPE_ALIPAY}
     */
    private Integer payType;
    /**
     * 订单状态
     */
    private Integer state;
    /**
     * 结束时间,成功充值或者关闭订单的时间
     */
    private Date endTime;
    /**
     * 充值金额，单位为分
     */
    private Integer amount;
    /**
     * 充值后房间账号剩余的账户余额
     */
    private Integer accountRemained;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getPrepareDate() {
        return prepareDate;
    }

    public void setPrepareDate(Date prepareDate) {
        this.prepareDate = prepareDate == null ? null : DateTimeUtil.setMilli(prepareDate,0);
    }

    public Integer getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Integer expiresIn) {
        this.expiresIn = expiresIn;
    }

    public Integer getPayType() {
        return payType;
    }

    public void setPayType(Integer payType) {
        this.payType = payType;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime == null ? null : DateTimeUtil.setMilli(endTime, 0);
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Integer getAccountRemained() {
        return accountRemained;
    }

    public void setAccountRemained(Integer accountRemained) {
        this.accountRemained = accountRemained;
    }

    public boolean success() {
        return STATE_PAID == state;
    }

    public boolean closed() {
        return STATE_CLOSED == state;
    }

    public boolean paying() {
        return STATE_PAYING == state;
    }

    public boolean payTypeAli(){
        return payType == PAY_TYPE_ALIPAY;
    }

    /**
     * 金额的字符串,以元为单位，2位小数
     * @return 金额字符串
     */
    public String amountStr(){
        int t = amount != null?amount:0;
        int n = t%100;
        return t / 100 + "." + (n < 10?"0" + n :String.valueOf(n));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ChargeOrderBase)) return false;
        ChargeOrderBase that = (ChargeOrderBase) o;
        return Objects.equals(id, that.id) && Objects.equals(prepareDate, that.prepareDate) && Objects.equals(expiresIn, that.expiresIn) && Objects.equals(payType, that.payType) && Objects.equals(state, that.state) && Objects.equals(endTime, that.endTime) && Objects.equals(amount, that.amount) && Objects.equals(accountRemained, that.accountRemained);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, prepareDate, expiresIn, payType, state, endTime, amount, accountRemained);
    }

    @Override
    public String toString() {
        return "ChargeOrderBase{" +
                "id=" + id +
                ", prepareDate=" + prepareDate +
                ", expiresIn=" + expiresIn +
                ", payType=" + payType +
                ", state=" + state +
                ", endTime=" + endTime +
                ", amount=" + amount +
                ", accountRemained=" + accountRemained +
                '}';
    }
}
